home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / wpj1_8.zip / FDIAG.ZIP / FILEDIAL.CPP < prev    next >
C/C++ Source or Header  |  1993-08-12  |  5KB  |  220 lines

  1. // filedial.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include "fdiag.h"
  7. #include "dlgs.h"        // define the resource in a file list dialog.
  8. #include "filedial.h"
  9. #include "string.h"
  10.  
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char BASED_CODE THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CFileDialogEx dialog
  18.  
  19. CFileDialogEx::CFileDialogEx(BOOL bOpenFileDialog, 
  20.         LPCSTR lpszDefExt, 
  21.         LPCSTR lpszFileName, 
  22.         DWORD dwFlags, 
  23.         LPCSTR lpszFilter, 
  24.         CWnd* pParentWnd)
  25.     : CFileDialog(bOpenFileDialog, 
  26.         lpszDefExt,          
  27.         lpszFileName, 
  28.         dwFlags |OFN_ENABLETEMPLATE |OFN_ALLOWMULTISELECT,
  29.         lpszFilter, 
  30.         pParentWnd
  31.     )
  32. {
  33.     //{{AFX_DATA_INIT(CFileDialogEx)
  34.     //}}AFX_DATA_INIT
  35.     
  36.     m_ofn.lpTemplateName = MAKEINTRESOURCE (IDD_FILEOPENORD);
  37.     m_ofn.hInstance = AfxGetResourceHandle();
  38.     m_bAdd = FALSE;
  39. }
  40.  
  41. CFileDialogEx::~CFileDialogEx()
  42. {
  43.     for(int i =0; i< m_saFileList.GetSize(); i++) {
  44.          ::free((char*) (const char*) m_saFileList[i]);
  45.     }
  46. };
  47. void CFileDialogEx::DoDataExchange(CDataExchange* pDX)
  48. {
  49.     CFileDialog::DoDataExchange(pDX);
  50.     //{{AFX_DATA_MAP(CFileDialogEx)
  51.     DDX_Control(pDX, IDC_BUTTON2, m_btnDelete);
  52.     DDX_Control(pDX, IDC_BUTTON1, m_btnAdd);
  53.     DDX_Control(pDX, IDC_LIST1, m_FileList);
  54.     //}}AFX_DATA_MAP
  55. }
  56.  
  57. BEGIN_MESSAGE_MAP(CFileDialogEx, CFileDialog)
  58.     //{{AFX_MSG_MAP(CFileDialogEx)
  59.     ON_WM_DESTROY()
  60.     ON_BN_CLICKED(IDC_BUTTON1, OnClickedButton1)
  61.     ON_BN_CLICKED(IDC_BUTTON2, OnClickedButton2)
  62.     //}}AFX_MSG_MAP
  63. END_MESSAGE_MAP()
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CFileDialogEx message handlers
  67. BOOL CFileDialogEx::OnInitDialog()
  68. {
  69.     CFileDialog::OnInitDialog();    
  70.     
  71.     return TRUE;  // return TRUE  unless you set the focus to a control
  72. }
  73.  
  74. BOOL CFileDialogEx::OnFileNameOK()
  75. {
  76.     if (m_bAdd)     
  77.         return TRUE;    // Keep the dialog.
  78.     else 
  79.         return FALSE;    
  80.         
  81.     // Handler for the LPOPENFILENAME;    
  82.     /*static BOOL bQuit = FALSE;
  83.     if (bQuit) {
  84.         bQuit = FALSE;
  85.         if (!m_bAdd) return FALSE;    // discontinue 
  86.         else return TRUE;        // continue
  87.     }
  88.     
  89.     int cbSel;
  90.     cbSel = 1 + *((LPWORD) m_ofn.lpstrFile);
  91.     LPSTR p = (LPSTR) ::malloc(cbSel);        // delet by the destructor
  92.     m_ofn.lpstrFile = p;
  93.     m_ofn.nMaxFile = cbSel;
  94.     
  95.     bQuit = TRUE;
  96.     SendMessage(WM_COMMAND, IDOK, 0L);
  97.     
  98.     return TRUE;
  99.     */
  100. }; 
  101.  
  102. static char* newStr(const char* s)
  103. {
  104.      char *p = (char*) ::malloc(strlen(s)+1);
  105.      strcpy(p, s);
  106.      return p;
  107. }
  108.  
  109. void CFileDialogEx::OnOK()
  110. {
  111.     // TODO: Add extra validation here    
  112.     CFileDialog::OnOK();      
  113.     
  114.     if (m_bAdd) return;        // don't do anything.
  115.     
  116.     // End Dialog regardless.
  117.     char sFileName[255];
  118.     for(int i =0; i< m_FileList.GetCount(); i++) {
  119.         m_FileList.GetText(i, sFileName);
  120.         m_saFileList.Add(newStr(sFileName));
  121.     }
  122.     EndDialog(IDOK);
  123. }
  124.  
  125. void CFileDialogEx::OnDestroy()
  126. {   
  127.     CFileDialog::OnDestroy();    
  128.     // TODO: Add your message handler code here    
  129. }
  130.  
  131. void CFileDialogEx::OnClickedButton1()
  132. {
  133.     // TODO: Add your control notification handler code here
  134.  
  135.     // force the control to put the path information in the
  136.     // m_ofn.lpstrFile.
  137.     m_bAdd = TRUE;
  138.     SendMessage(WM_COMMAND, IDOK, 0L);
  139.     m_bAdd = FALSE;
  140.     
  141.     char fileName[256];    
  142.     CString pathName;
  143.     
  144.     int nSelCount = (int) SendDlgItemMessage(lst1, LB_GETSELCOUNT, 0, 
  145.         (LPARAM)0);
  146.         
  147.     if (nSelCount ==0) {
  148.         MessageBox("Nothing to Add!", "WARNING", 
  149.             MB_ICONEXCLAMATION|MB_OK);
  150.         return;
  151.     }
  152.     CString s;
  153.     // only one item is selected.
  154.     if (nSelCount==1) {
  155.         s = m_ofn.lpstrFile;
  156.         s.MakeLower();
  157.         if (m_FileList.FindString(0, s)==LB_ERR) 
  158.             m_FileList.AddString(s);
  159.         else {                         
  160.             CString sWarning = "Duplicated filename " + s + "!";            
  161.             MessageBox(sWarning, "WARNING", 
  162.             MB_ICONEXCLAMATION|MB_OK);
  163.         }
  164.         return;
  165.     }
  166.     
  167.     char* path = strtok((char*)m_ofn.lpstrFile, " ");
  168.     pathName = path;        
  169.  
  170.     int* lpSelItems = (int*) ::malloc(sizeof(int) * (nSelCount+1));
  171.     int ret = (int) SendDlgItemMessage(lst1, LB_GETSELITEMS, 
  172.         (WPARAM) nSelCount, (LPARAM) lpSelItems);
  173.  
  174.  
  175.     pathName += "\\";        
  176.     
  177.     ASSERT(ret!=LB_ERR);    
  178.     int curIdx = 0;
  179.     
  180.     for (int i = 0; i < nSelCount; i++) {        
  181.         curIdx = lpSelItems[i];
  182.         SendDlgItemMessage(lst1, LB_GETTEXT, curIdx, (LPARAM) (LPSTR) fileName);
  183.         s = pathName;        
  184.         s += fileName;
  185.         // add string if it's not in the listbox already
  186.         s.MakeLower();
  187.         if (m_FileList.FindString(0, s)==LB_ERR) 
  188.             m_FileList.AddString(s);
  189.         else {                         
  190.             CString sWarning = "Duplicated filename " + s + "!";
  191.             
  192.             MessageBox(sWarning, "WARNING", 
  193.             MB_ICONEXCLAMATION|MB_OK);
  194.         }
  195.             // TRACE("Duplicated filename %s\n", (LPCSTR)s);
  196.     }
  197.     ::free(lpSelItems);
  198. }
  199.  
  200. void CFileDialogEx::OnClickedButton2()
  201. {
  202.     // TODO: Add your control notification handler code here
  203.     int nSelCount = m_FileList.GetSelCount();
  204.     if (nSelCount ==0) {
  205.         MessageBox("Nothing to Delete!", "WARNING", 
  206.             MB_ICONEXCLAMATION|MB_OK);
  207.         return;
  208.     };
  209.     int* lpSelItems = (int*) ::malloc(sizeof(int) * (nSelCount+1));
  210.     int ret = m_FileList.GetSelItems(nSelCount, lpSelItems);
  211.     ASSERT(ret!=LB_ERR);
  212.     int curIdx = 0;
  213.     for (int i = nSelCount-1;  i >=0;  i--) {
  214.         curIdx = lpSelItems[i];
  215.         m_FileList.DeleteString(curIdx);
  216.     };
  217.     ::free(lpSelItems);
  218. }
  219.  
  220.